package com.souku.finance.application.ui.more;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.ViewPagerAdapter;
import com.souku.finance.application.adapter.VipTaskAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.MemberinfoBean;
import com.souku.finance.application.bean.TasksBean;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.RoundImageView;
import com.souku.finance.application.widget.ViewPagerIndicator;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/21.
 */

public class VipActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ViewPagerIndicator idIndicator;
    protected ViewPager idVp;
    protected RoundImageView ivHead;
    protected TextView tvPhone;
    protected TextView tvLevel;
    protected ProgressBar oseekbar;
    protected TextView tvTextprogres;
    protected TextView tvScore;
    protected TextView tvUsescore;
    private View view1;
    private View view2;
    private PullToRefreshListView pullToRefreshListView;
    private VipTaskAdapter vipTaskAdapter;
    private List<TasksBean.DataBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_vip);
        initView();
        topTitle.setText("会员中心");
        view1 = LayoutInflater.from(this).inflate(R.layout.page_task, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.page_vipfree, null);
        pullToRefreshListView = (PullToRefreshListView) view1.findViewById(R.id.task_pull_list);
        final ArrayList<View> views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        idVp.setAdapter(adapter);
        idIndicator.setViewPager(idVp, 0);
        vipTaskAdapter = new VipTaskAdapter(list, new VipTaskAdapter.OnClick() {
            @Override
            public void click(boolean isreceive, String id) {
                if (isreceive) {
                    receive_member_task(id);
                } else {
                    receive_member_task(id);
                }
            }
        });
        pullToRefreshListView.setAdapter(vipTaskAdapter);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                getMemberTasks();
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setHeadImage(ShareUtil.getInstance().getString(Constant.AvatarUrl));
        getMemberinfo();
        pullToRefreshListView.setRefreshing(true);
    }

    @Override
    public void internet() {
        getMemberinfo();
        pullToRefreshListView.setRefreshing(true);
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(VipActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        idIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
        idVp = (ViewPager) findViewById(R.id.id_vp);
        ivHead = (RoundImageView) findViewById(R.id.iv_head);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvLevel = (TextView) findViewById(R.id.tv_level);
        oseekbar = (ProgressBar) findViewById(R.id.oseekbar);
        tvTextprogres = (TextView) findViewById(R.id.tv_textprogres);
        tvScore = (TextView) findViewById(R.id.tv_score);
        tvUsescore = (TextView) findViewById(R.id.tv_usescore);
    }

    private void getMemberinfo() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        RequestManager.getInstance().post(UrlConstact.MEMBER_INFO, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    MemberinfoBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), MemberinfoBean.class);
                    setData(bean);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void getMemberTasks() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("userId", ShareUtil.getInstance().getString(Constant.UserId));
        RequestManager.getInstance().post(UrlConstact.QUERY_MEMBER_TASKS, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                pullToRefreshListView.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    TasksBean bean = JsonUtil.fastBean(response, TasksBean.class);
                    list.clear();
                    list.addAll(bean.getData());
                    vipTaskAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void receive_member_task(String id) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("task_id", id);
        RequestManager.getInstance().post(UrlConstact.RECEIVE_MEMBER_TASK, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("领取成功");
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void setData(MemberinfoBean bean) {
        tvPhone.setText(ShareUtil.getInstance().getString(Constant.Mobile));
        tvLevel.setText("Lv" + bean.getMemberLevel());
        tvScore.setText(bean.getCumulatedPoint());
        tvUsescore.setText(bean.getAvailablePoint());
//        int max = Integer.parseInt(bean.getLevelMaxPoint()) - Integer.parseInt(bean.getLevelMinPoint());
//        int current = Integer.parseInt(bean.getCumulatedPoint()) - Integer.parseInt(bean.getLevelMinPoint());
//        tvTextprogres.setText(current + "/" + max);
        int levelMinute = Integer.parseInt(bean.getLevelMinute());
        int cumulatedPoint = Integer.parseInt(bean.getCumulatedPoint());
        oseekbar.setMax(levelMinute);//等级积分
        oseekbar.setProgress(cumulatedPoint);//累计积分
        tvTextprogres.setText(cumulatedPoint + "/" + levelMinute);
    }

    //头像
    private void setHeadImage(String url) {
        if (MyTextUtil.isEmpty(url)) {
            ivHead.setImageResource(R.drawable.touxiang);
        } else {
            ImageLoader.getInstance().displayImage(url, ivHead);
        }
    }
}
